
type commonT = {
  _type: string,
  image_path: string,
}

// 身份证信息
export type idCardT = {
  name: string,
  gender: string,
  nationality: string,
  address: string,
  id: string,
} & commonT

export type idCardBackT = {
  authority: string,
  valid_date: string,
} & commonT;

// 通用的只有一个编码的证件
export type numberT = {
  number: string
} & commonT

// 行驶证
export type drivingT = {
  plate_num: string,
  vehicle_type: string,
  owner: string,
  addr: string,
  use_character: string,
  model: string,
  vin: string,
  engine_num: string,
  register_date: string,
  issue_date: string,
  plate_num_b: string,
  record: string,
  passengers_num: string,
  total_quality: string,
  prepare_quality: string,
  load_quality: string,
  lead_quality: string,
  overall_size: string,
} & commonT

// 营业执照
export type businessT = {
  reg_num: string,
  legal_representative: string,
  enterprise_name: string,
  address: string,
  type_of_enterprise: string,
  business_scope: string,
  registered_capital: string,
  valid_period: string,
  registered_date: string,
  cert_position: string,
} & commonT

// 驾驶证
export type driverT = {
  reg_num: string,
  legal_representative: string,
  enterprise_name: string,
  address: string,
} & commonT

// 菜单
export type menuT = {
  menu: {
    menu_name: string,
    menu_price: number,
  }[]
} & commonT

export type cardT = idCardT | idCardBackT | numberT | driverT | drivingT | businessT | menuT | null;

export type historyT = {
  card: cardT,
  keyword: string | number,
  descript: string,
}

export type cardsT = cardT[] | null

type recordT = {
  [key:string]: string | number
}

type resultT = {
  [key:string]: recordT & string
}

// 显示卡片的主键信息
export function cardKeyword(result: cardT): string | number {
  switch (result?._type) {
    case 'idCard':
      return (result as idCardT).id
    case 'idCardBack':
      return (result as idCardBackT).authority
    case 'bankCard':
      return (result as numberT).number
    case 'driverslicense':
      return (result as driverT).enterprise_name
    case 'drivingLicense':
      return (result as drivingT).vehicle_type
    case 'businessLicense':
      return (result as businessT).enterprise_name
    case 'platenum':
      return (result as numberT).number
    case 'menu':
      return '菜单'
  }
  return "未知"
}

// 卡片的描述信息
export function cardDescript(result: cardT): string {
  switch (result?._type) {
    case 'idCard':
      {
        let res = result as idCardT
        return `${res.name} ${res.gender} ${res.nationality} ${res.address}`
      }
    case 'idCardBack':
      {
        let res = (result as idCardBackT)
        return `${res.authority} ${res.valid_date}`
      }
    case 'bankCard':
      {
        let res = result as numberT
        return `${res.number}`
      }
    case 'driverslicense':
      {
        let res = result as driverT
        return `${res.enterprise_name} ${res.address}`
      }
    case 'drivingLicense':
      {
        let res = result as drivingT
        return `${res.owner} ${res.plate_num} ${res.model}`
      }
    case 'businessLicense':
      {
        let res = result as businessT
        return `${res.reg_num} ${res.address}`
      }
    case 'platenum':
      return (result as numberT).number
    case 'menu':
      {
        let res = result as menuT
        let menus: string[] = []
        res.menu.forEach(item => {
          menus.push(`${item.menu_name}（${item.menu_price}）`)
        })
        return menus.join("；")
      }
  }
  return "未知"
}

export function parseCardResult(e: WechatMiniprogram.CustomEvent): cardT | null {
  let type = e.currentTarget.dataset['type']
  console.log(type)
  if (!type) {
    return null
  }

  let result: cardT = null
  switch (type) {
    case 'idCard':
      result = parseIdCard(e.detail, !!e.currentTarget.dataset['oppo'])
      break;
    case 'bankCard':
      result = parseBankCard(e.detail)
      break;
    case 'driverslicense':
      result = parseDriver(e.detail)
      break;
    case 'drivingLicense':
      result = parseDriving(e.detail)
      break;
    case 'businessLicense':
      result = parseBusiness(e.detail)
      break;
    case 'platenum':
      result = parsePlatenum(e.detail)
      break;
    case 'menu':
      result = parseMenu(e.detail)
      break;
  }
  return result
}

export function parseIdCard(result: resultT, oppo: boolean = false): idCardT | idCardBackT | null {
  if (!result) {
    return null
  }
  // 反面
  if (oppo) {
    return {
      _type: 'idCardBank',
      image_path: result.image_path as string,
      authority: result.authority.text as string,
      valid_date: result.valid_date.text as string,
    }
  }
  return {
    _type: 'idCard',
    name: result.name.text as string,
    gender: result.gender?.text as string,
    nationality: result.nationality?.text as string,
    address: result.address?.text as string,
    id: result.id?.text as string,
    image_path: result.image_path as string,
  }
}

export function parseIdCardback(result: Object): idCardBackT | null {
  console.log(result)
  return null
}

export function parseDriving(result: resultT): drivingT | null {
  if (!result) {
    return null
  }

  return {
    _type: 'drivingLicense',
    plate_num: result.plate_num?.text as string,
    vehicle_type: result.vehicle_type?.text as string,
    owner: result.owner?.text as string,
    addr: result.addr?.text as string,
    use_character: result.use_character?.text as string,
    model: result.model?.text as string,
    vin: result.vin?.text as string,
    engine_num: result.engine_num?.text as string,
    register_date: result.register_date?.text as string,
    issue_date: result.issue_date?.text as string,
    plate_num_b: result.plate_num_b?.text as string,
    record: result.record?.text as string,
    passengers_num: result.passengers_num?.text as string,
    total_quality: result.total_quality?.text as string,
    prepare_quality: result.prepare_quality?.text as string,
    load_quality: result.load_quality?.text as string,
    lead_quality: result.lead_quality?.text as string,
    overall_size: result.overall_size?.text as string,
    image_path:result.image_path as string,
  }
}

export function parseDriver(result: resultT): driverT | null {
  console.log(result)
  return null
}

export function parseBusiness(result: resultT): businessT | null {
  if (!result) {
    return null
  }

  return {
    _type: 'businessLicense',
    reg_num: result.reg_num?.text as string,
    legal_representative: result.legal_representative?.text as string,
    enterprise_name: result.enterprise_name?.text as string,
    address: result.address?.text as string,
    type_of_enterprise: result.type_of_enterprise?.text as string,
    business_scope: result.business_scope?.text as string,
    registered_capital: result.registered_capital?.text as string,
    valid_period: result.valid_period?.text as string,
    registered_date: result.registered_date?.text as string,
    cert_position: result.cert_position?.text as string,
    image_path: result.image_path as string,
  }
}

export function parseMenu(result: resultT): menuT | null {
  if (!result) {
    return null
  }
  return {
    _type: 'menu',
    menu: [],
    image_path: result.image_path as string,
  }
}

export function parseBankCard(result: resultT): numberT | null {
  if (!result || !result.number) {
    return null
  }

  return {
    _type: 'bankCard',
    number: result.number?.text as string,
    image_path: result.image_path as string,
  }
}

export function parsePlatenum(result: resultT): numberT | null {
  if (!result) {
    return null
  }

  return {
    _type: 'platenum',
    number: result.number?.text as string,
    image_path: result.image_path as string,
  }
}